import { create } from "zustand";

// 数据持久化
import { createJSONStorage, persist } from "zustand/middleware";

function asyncFeatData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(200);
    }, 2000);
  });
}

//useAppStore==vue中的state
const useAppStore = create(
  //持久化储存
  persist(
    (set, get) => ({
      active:'home',
      //数据
      //同步操作
      // addCount: () => set((state) => ({ count: state.count + 1 })),
      // reduce: () => set((state) => ({ count: state.count - 1 })),
      setActive:(data)=>set(()=>({active:data})),
      //异步操作获取数据
      // getData: async () => {
      //   let res = await asyncFeatData();
      //   console.log(res, "res");
      //   set({ count: res });
      // },
      //获取state的数据包括方法
      // dbCount: () => {
      //   let val = get()?.count * 2;
      //   console.log(val, "val");
      //   set({ count: val });
      // },
    }),
    // 关键点在这里初始化就会将state储存进storage
    {
      //存进storage的名字
      name: "globalState",
      //值可选sessionStorage，localStorage
      storage: createJSONStorage(() => localStorage),
    }
  )
);

export default useAppStore;
